#include "stdafx.h"
#include "MAPITable.h"
#include "MAPIFolder.h"
#include <MAPICode.h>

// #include <mapival.h>
// #include "MAPIHelper.h"

#define INITGUID
#define USES_IID_IMAPISup
#define USES_IID_IMAPIFolder
#define USES_IID_IMAPIProp
#define USES_IID_IMAPIContainer
#include <initguid.h>
#include <mapiguid.h>

#define Log

//-----------------------------------------------------------------------
// Constructor
CMAPIFolder::CMAPIFolder( LPMAPIFOLDER pObj ) : m_pObj(pObj), m_cRef(1)
{
	Log(true, "CMAPIFolder::CMAPIFolder method called!");
	m_pObj->AddRef();
}


//-----------------------------------------------------------------------
// Destructor
CMAPIFolder::~CMAPIFolder(void)
{
	Log(true, "CMAPIFolder::~CMAPIFolder method called!");
	m_pObj->Release();
}


//////////////////////////////////////////////////////////////////////////
// Implementation IUnknown
STDMETHODIMP CMAPIFolder::QueryInterface (REFIID riid, LPVOID * ppvObj)
{
	Log( true, "CMAPIFolder::QueryInterface method called!" );
	*ppvObj = NULL;

	if ( riid == IID_IMAPIFolder ||
		riid == IID_IMAPIContainer ||
		riid == IID_IMAPIProp ||
		riid == IID_IUnknown ) {
			*ppvObj = (LPVOID)this;
			AddRef();
			return S_OK;
	} else {
		*ppvObj = NULL;
		return E_NOINTERFACE;
	}
}


STDMETHODIMP_(ULONG) CMAPIFolder::AddRef ()
{
	Log(true, "CMAPIFolder::AddRef method called!");
	if ( !m_pObj ) {
		return NULL;
	}

	++m_cRef;
	return m_cRef;
}


STDMETHODIMP_(ULONG) CMAPIFolder::Release ()
{
	Log(true, "CMAPIFolder::Release method called!");
	if (!m_pObj) {
		return NULL;
	}

	--m_cRef;
	if (m_cRef == 0) {
		delete this;
		return 0;
	}

	return m_cRef;
}



//////////////////////////////////////////////////////////////////////////
// Implementation IMAPIProp interface.
//
HRESULT CMAPIFolder::CopyProps(
							   LPSPropTagArray lpIncludeProps,
							   ULONG_PTR ulUIParam,
							   LPMAPIPROGRESS lpProgress,
							   LPCIID lpInterface,
							   LPVOID lpDestObj,
							   ULONG ulFlags,
							   LPSPropProblemArray FAR * lppProblems )
{
	Log(true, "CMAPIFolder::CopyProps method called!");
	HRESULT hRes = S_OK;
	if ( m_pObj ) {
		hRes = m_pObj->CopyProps(
			lpIncludeProps,
			ulUIParam,
			lpProgress,
			lpInterface,
			lpDestObj,
			ulFlags,
			lppProblems );
		return hRes;
	}
	return MAPI_E_NO_SUPPORT;
}



HRESULT CMAPIFolder::CopyTo(
							ULONG ciidExclude,
							LPCIID rgiidExclude,
							LPSPropTagArray lpExcludeProps,
							ULONG_PTR ulUIParam,
							LPMAPIPROGRESS lpProgress,
							LPCIID lpInterface,
							LPVOID lpDestObj,
							ULONG ulFlags,
							LPSPropProblemArray FAR * lppProblems
							)
{
	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::DeleteProps(
								 LPSPropTagArray lpPropTagArray,
								 LPSPropProblemArray FAR * lppProblems
								 )
{
	Log(true, "CMAPIFolder::DeleteProps method called!");
	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::GetIDsFromNames(
									 ULONG cPropNames,
									 LPMAPINAMEID FAR * lppPropNames,
									 ULONG ulFlags,
									 LPSPropTagArray FAR * lppPropTags )
{
	Log(true, "CMAPIFolder::GetIDsFromNames method called!");
	HRESULT hRes = S_OK;
	if ( m_pObj ) {
		hRes = m_pObj->GetIDsFromNames(
			cPropNames,
			lppPropNames,
			ulFlags,
			lppPropTags );
		return hRes;
	}
	return MAPI_E_NO_SUPPORT;
}

HRESULT CMAPIFolder::GetLastError(
								  HRESULT hResult,
								  ULONG ulFlags,
								  LPMAPIERROR FAR * lppMAPIError
								  )
{
	Log(true, "CMAPIFolder::GetLastError method called!");
	HRESULT hRes = S_OK;
	if ( m_pObj ) {
		hRes = m_pObj->GetLastError(
			hResult,
			ulFlags,
			lppMAPIError);
		return hRes;
	}
	return MAPI_E_NO_SUPPORT;
}

HRESULT CMAPIFolder::GetNamesFromIDs(
									 LPSPropTagArray FAR * lppPropTags,
									 LPGUID lpPropSetGuid,
									 ULONG ulFlags,
									 ULONG FAR * lpcPropNames,
									 LPMAPINAMEID FAR * FAR * lpppPropNames
									 )
{
	Log(true, "CMAPIFolder::GetNamesFromIDs method called");
	HRESULT hRes = S_OK;
	if ( m_pObj ) {
		hRes = m_pObj->GetNamesFromIDs(
			lppPropTags,
			lpPropSetGuid,
			ulFlags,
			lpcPropNames,
			lpppPropNames );
		return hRes;
	}
	return MAPI_E_NO_SUPPORT;
}

HRESULT CMAPIFolder::GetPropList(
								 ULONG ulFlags,
								 LPSPropTagArray FAR * lppPropTagArray
								 )
{
	Log(true, "CMAPIFolder::GetPropList method called");
	HRESULT hRes = S_OK;
	if ( m_pObj ) {
		hRes = m_pObj->GetPropList(
			ulFlags,
			lppPropTagArray );
		return hRes;
	}
	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::GetProps(
							  LPSPropTagArray lpPropTagArray,
							  ULONG ulFlags,
							  ULONG FAR * lpcValues,
							  LPSPropValue FAR * lppPropArray
							  )
{
	Log(true, "CMAPIFolder::GetProps method called");
	HRESULT hRes = S_OK;
	if ( m_pObj ) {
		hRes = m_pObj->GetProps(
			lpPropTagArray,
			ulFlags,
			lpcValues,
			lppPropArray );
		return hRes;
	}
	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::OpenProperty(
								  ULONG ulPropTag,
								  LPCIID lpiid,
								  ULONG ulInterfaceOptions,
								  ULONG ulFlags,
								  LPUNKNOWN FAR * lppUnk
								  )
{
	Log(true, "CMAPIFolder::OpenProperty method called");
	HRESULT hRes = S_OK;
	if ( m_pObj ) {
		hRes = m_pObj->OpenProperty(
			ulPropTag,
			lpiid,
			ulInterfaceOptions,
			ulFlags,
			lppUnk );
		return hRes;
	}
	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::SaveChanges(
								 ULONG ulFlags
								 )
{
	Log(true, "CMAPIFolder::SaveChanges method called");
	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::SetProps(
							  ULONG cValues,
							  LPSPropValue lpPropArray,
							  LPSPropProblemArray FAR * lppProblems
							  )
{
	Log(true, "CMAPIFolder::SetProps method called");
	return MAPI_E_NO_SUPPORT;
}

//////////////////////////////////////////////////////////////////////////
// Implement IMAPIContainer interface
//
HRESULT CMAPIFolder::GetContentsTable(
									  ULONG ulFlags,
									  LPMAPITABLE FAR * lppTable
									  )
{
	Log(true, "CMAPIFolder::GetContentsTable method called");
	HRESULT hRes = S_OK;
	if ( m_pObj ) {
		hRes = m_pObj->GetContentsTable(
			ulFlags,
			lppTable );
		return hRes;
	}
	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::GetHierarchyTable(
									   ULONG ulFlags,
									   LPMAPITABLE FAR * lppTable
									   )
{
	Log(true, "CMAPIFolder::GetHierarchyTable method called!");
	HRESULT hRes = S_OK;
	LPMAPITABLE lpTable = NULL;

	if ( m_pObj ) {
		hRes = m_pObj->GetHierarchyTable(
			ulFlags,
			&lpTable );
		if ( lpTable ) {
			*lppTable = new CMAPITable( lpTable );
		}
		return hRes;
	}

	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::GetSearchCriteria(
									   ULONG ulFlags,
									   LPSRestriction FAR * lppRestriction,
									   LPENTRYLIST FAR * lppContainerList,
									   ULONG FAR * lpulSearchState
									   )
{
	Log(true, "CMAPIFolder::GetSearchCriteria method called");
	return MAPI_E_NO_SUPPORT;
}



HRESULT CMAPIFolder::OpenEntry(
							   ULONG cbEntryID,
							   LPENTRYID lpEntryID,
							   LPCIID lpInterface,
							   ULONG ulFlags,
							   ULONG FAR * lpulObjType,
							   LPUNKNOWN FAR * lppUnk
							   )
{
	Log(true, "CMAPIFolder::OpenEntry method called!");

	HRESULT hRes = S_OK;
// 	hRes = ::OpenEntry( m_pObj, cbEntryID, lpEntryID, lpInterface, ulFlags, lpulObjType, lppUnk );
	if ( m_pObj ) {
		hRes = m_pObj->OpenEntry(
			cbEntryID,
			lpEntryID,
			lpInterface,
			ulFlags,
			lpulObjType,
			lppUnk );
		return hRes;
	}
	Log(true, "CMAPIFolder::OpenEntry method return. Type is: %d", *lpulObjType);

	return hRes;
}

HRESULT CMAPIFolder::SetSearchCriteria(
									   LPSRestriction lpRestriction,
									   LPENTRYLIST lpContainerList,
									   ULONG ulSearchFlags
									   )
{
	Log(true, "CMAPIFolder::SetSearchCriteria method called");
	return MAPI_E_NO_SUPPORT;
}




//////////////////////////////////////////////////////////////////////////
// Implement IMAPIFolder interface.
//

HRESULT CMAPIFolder::CopyFolder(
								ULONG cbEntryID,
								LPENTRYID lpEntryID,
								LPCIID lpInterface,
								LPVOID lpDestFolder,
								LPTSTR lpszNewFolderName,
								ULONG_PTR ulUIParam,
								LPMAPIPROGRESS lpProgress,
								ULONG ulFlags )
{
	Log(true,"CMAPIFolder::CopyFolder method called");
// 	Validate_IMAPIFolder_CopyFolder(
// 		this,
// 		cbEntryID,
// 		lpEntryID,
// 		lpInterface,
// 		lpDestFolder,
// 		lpszNewFolderName,
// 		ulUIParam,
// 		lpProgress,
// 		ulFlags );

	return MAPI_E_NO_SUPPORT;
}

//-----------------------------------------------------------------------
// Copies or moves one or more messages.
//
HRESULT CMAPIFolder::CopyMessages(
								  LPENTRYLIST lpMsgList,
								  LPCIID lpInterface,
								  LPVOID lpDestFolder,
								  ULONG_PTR ulUIParam,
								  LPMAPIPROGRESS lpProgress,
								  ULONG ulFlags )
{
	Log(true,"CMAPIFolder::CopyMessages method called");
// 	Validate_IMAPIFolder_CopyMessages(
// 		this,
// 		lpMsgList,
// 		lpInterface,
// 		lpDestFolder,
// 		ulUIParam,
// 		lpProgress,
// 		ulFlags );

	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::CreateFolder(
								  ULONG ulFolderType,
								  LPTSTR lpszFolderName,
								  LPTSTR lpszFolderComment,
								  LPCIID lpInterface,
								  ULONG ulFlags,
								  LPMAPIFOLDER FAR * lppFolder )
{
	Log(true,"CMAPIFolder::CreateFolder method called!");
// 	Validate_IMAPIFolder_CreateFolder(
// 		this,
// 		ulFolderType,
// 		lpszFolderName,
// 		lpszFolderComment,
// 		lpInterface,
// 		ulFlags,
// 		lppFolder );

	HRESULT hRes = S_OK;

	if ( m_pObj ) {
		hRes = m_pObj->CreateFolder(
			ulFolderType,
			lpszFolderName,
			lpszFolderComment,
			lpInterface,
			ulFlags,
			lppFolder );

		return hRes;
	}

	return MAPI_E_NO_SUPPORT;
}


//-----------------------------------------------------------------------
// Creates a new message.
//
HRESULT CMAPIFolder::CreateMessage(
								   LPCIID lpInterface,
								   ULONG ulFlags,
								   LPMESSAGE FAR * lppMessage )
{
	Log(true,"CMAPIFolder::CreateMessage method called");
// 	Validate_IMAPIFolder_CreateMessage(
// 		this,
// 		lpInterface,
// 		ulFlags,
// 		lppMessage );

	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::DeleteFolder(
								  ULONG_PTR cbEntryID,
								  LPENTRYID lpEntryID,
								  ULONG_PTR ulUIParam,
								  LPMAPIPROGRESS lpProgress,
								  ULONG ulFlags )
{
	Log(true, "CMAPIFolder::DeleteFolder method called!");
// 	Validate_IMAPIFolder_DeleteFolder(
// 		this,
// 		cbEntryID,
// 		lpEntryID,
// 		ulUIParam,
// 		lpProgress,
// 		ulFlags );

	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::DeleteMessages(
									LPENTRYLIST lpMsgList,
									ULONG_PTR ulUIParam,
									LPMAPIPROGRESS lpProgress,
									ULONG ulFlags )
{
	Log(true, "CMAPIFolder::DeleteMessages method called");
// 	Validate_IMAPIFolder_DeleteMessages(
// 		this,
// 		lpMsgList,
// 		ulUIParam,
// 		lpProgress,
// 		ulFlags );

	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::EmptyFolder(
								 ULONG_PTR ulUIParam,
								 LPMAPIPROGRESS lpProgress,
								 ULONG ulFlags )
{
	Log(true, "CMAPIFolder::EmptyFolder method called");
// 	Validate_IMAPIFolder_EmptyFolder(
// 		this,
// 		ulUIParam,
// 		lpProgress,
// 		ulFlags );

	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::GetMessageStatus(
									  ULONG cbEntryID,
									  LPENTRYID lpEntryID,
									  ULONG ulFlags,
									  ULONG FAR * lpulMessageStatus )
{
	Log(true, "CMAPIFolder::GetMessageStatus method called");
// 	Validate_IMAPIFolder_GetMessageStatus(
// 		this,
// 		cbEntryID,
// 		lpEntryID,
// 		ulFlags,
// 		lpulMessageStatus );

	HRESULT hRes = S_OK;

	if ( m_pObj ) {
		hRes = m_pObj->GetMessageStatus(
			cbEntryID,
			lpEntryID,
			ulFlags,
			lpulMessageStatus );
		return hRes;
	}

	return MAPI_E_NO_SUPPORT;
}

HRESULT CMAPIFolder::SaveContentsSort(
									  LPSSortOrderSet lpSortCriteria,
									  ULONG ulFlags )
{
	Log(true, "CMAPIFolder::SaveContentsSort method called");
// 	Validate_IMAPIFolder_SaveContentsSort(
// 		this,
// 		lpSortCriteria,
// 		ulFlags );

	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::SetMessageStatus(
									  ULONG cbEntryID,
									  LPENTRYID lpEntryID,
									  ULONG ulNewStatus,
									  ULONG ulNewStatusMask,
									  ULONG FAR * lpulOldStatus )
{
	Log(true, "CMAPIFolder::SetMessageStatus method called");
// 	Validate_IMAPIFolder_SetMessageStatus(
// 		this,
// 		cbEntryID,
// 		lpEntryID,
// 		ulNewStatus,
// 		ulNewStatusMask,
// 		lpulOldStatus );

	return MAPI_E_NO_SUPPORT;
}


HRESULT CMAPIFolder::SetReadFlags(
								  LPENTRYLIST lpMsgList,
								  ULONG_PTR ulUIParam,
								  LPMAPIPROGRESS lpProgress,
								  ULONG ulFlags )
{
	Log(true, "CMAPIFolder::SetReadFlags method called");
// 	Validate_IMAPIFolder_SetReadFlags(
// 		this,
// 		lpMsgList,
// 		ulUIParam,
// 		lpProgress,
// 		ulFlags );

	return MAPI_E_NO_SUPPORT;
}

